<?php
namespace app\index\controller;
use think\Controller;

class Index extends Controller
{
    public function autologin()
    {   
    	if((request() -> isGet()) && (input('get.info'))){
			$uid = input('get.info');
    		$info =  db('common_member')->field('uid,extgroupids,username,groupexpiry')->where('username',$uid)->find();
    		if(!$info){
    			$this->error('用户不存在,不能进行自动登录',"http://".$_SERVER['HTTP_HOST']);
    		}
    		if(!$info['extgroupids']){
    			$this->error('还没购买用户组',"http://".$_SERVER['HTTP_HOST']);
    		}
    		$time = time();
    		$times = $info['groupexpiry'];
    		if($time > $times){
    			$this->error('购买时间已到期', "http://".$_SERVER['HTTP_HOST']);
	        }else{
	        	session('uids',$info['uid']);
    	    	session('usernames',$info['username']);
	        }
    	}
        return $this->fetch();
    }

    public function left()
    {
    	if(!session('uids')) {
	    	$this->redirect('index/personalogin');
	    }
        //分享信息遍历
        $cate = db('common_block_cate')->field('id,name')->select();
		if($cate){
			$arr = array();
			foreach ($cate as $key => $value) {
				$arr[] = $value['id'];
				$url =  db('common_block_shareurl')->where('cid',$value['id'])->select();
				foreach ($url as $k => $v) {
				   $msg['uid'] = session('uids');
				   $msg['sid'] = $v['id'];
				   $state = db('common_collect')->where($msg)->find();
				   if($state){
				   	    $url[$k]['state'] = 1;
				   }else{
				   	    $url[$k]['state'] = 0;
				   }
				}
				$cate[$key]['share'] = $url;
			}
			$arr = join(',',$arr);
			$urls =  db('common_block_shareurl')->where('cid','not in',$arr)->select();
			if($urls){
				foreach ($urls as $ke => $va) {
					$msg['uid'] = session('uids');
				    $msg['sid'] = $va['id'];
				    $state = db('common_collect')->where($msg)->find();
				    if($state){
				   	    $urls[$ke]['state'] = 1;
				    }else{
				   	    $urls[$ke]['state'] = 0;
				    }
				}
			}
		}
	    //我的收藏
	    $data['uid'] = session('uids');
		$coll = db('common_collect')->field('id,sid,titles')->where($data)->order('id desc')->select();
		foreach ($coll as $key => $value) {
			$list = db('common_block_shareurl')->field('title')->where('id',$value['sid'])->find();
			if($value['titles'] == ""){
				$coll[$key]['titles'] = $list['title'];
			}
		}
		$this->assign('coll',$coll);
		$this->assign('cate',$cate);
		$this->assign('cates',$urls);
        return $this->fetch();
    }

    public function urlid($id){
        $cate = db('common_block_shareurl')->select();  
        foreach ($cate as $k => $v){
            if($v['cid'] != $id){
              return $v;
            }
        }
    }

    public function example2()
    {
    	//最新帖子
    	$thread = db('forum_thread')->field('tid,subject')->order('tid desc')->limit(7)->select();
    	//公告
    	$notice = db('common_notice')->field('content')->limit(1)->order('id desc')->select();
    	//最新共享
        $new = db('common_block_shareurl')->field('id,title,num')->order('id desc')->limit(7)->select();
    	//热门共享排行
    	$top = db('common_block_shareurl')->field('id,title,num')->order('num desc,id desc')->limit(100)->select();

    	$this->assign('thread',$thread);
    	$this->assign('notice',$notice);
    	$this->assign('top',$top);
    	$this->assign('new',$new);
        return $this->fetch();
    }

    public function example3()
    {
        return $this->fetch();
    }
    
    //登录页面
    public function personalogin()
    {
    	if(session('uids')) {
	    	$this->redirect('index/autologin');
	    }
        return $this->fetch();
    }
    
    //验证登录
    public function login(){
    	if(request() -> isPost()){
	        $username = trim(input('post.username'));
	        $password = trim(input('post.password'));
	        $res1 = db('ucenter_members')->field('password,salt')->where('username',$username)->find();
	        if(!$res1){
	        	return json(['code'=>0,'msg'=>'用户名错误']);
	        }
	        $passwords = md5(md5($password).$res1['salt']);
	        if($passwords != $res1['password']){
	        	return json(['code'=>0,'msg'=>'密码错误']);
	        }
	        $res2 = db('common_member')->field('uid,extgroupids,username,groupexpiry')->where('username',$username)->find();
	        if(!$res2['extgroupids']){
	        	return json(['code'=>0,'msg'=>'还没购买用户组']);
	        }
	        $times = $res2['groupexpiry'];
	        $time = time();
	        if($time > $times){
	        	return json(['code'=>0,'msg'=>'购买时间已到期']);
	        }else{
	        	session('uids',$res2['uid']);
    	    	session('usernames',$res2['username']);
	        	return json(['code'=>1,'msg'=>'登录成功']);
	        }
    	}
    }

    //后台管理用户退出
	public function loginout()
	{
		// 清除session (当前作用域)
		session(null);
		echo exit('<script>top.location.href="autologin"</script>');
	}

	//用户收藏网址
	public function coll(){
		if(request() -> isPost()){
		   $data['sid'] = input('post.sid');
		   $data['uid'] = session('uids');
		   $cos = db('common_collect')->where($data)->find();
		   $coss = db('common_block_shareurl')->field('id,title')->where('id',$data['sid'])->find();
		   if($cos){
		   	  return json(['code'=>0,'msg'=>'已经收藏']);
		   }
		   $data['cid'] = input('post.cid');
		   $data['addtime'] = date('Y-m-d H:i:s');
		   $coll = db('common_collect')->insertGetId($data);
		   if($coll){
                return json(['code'=>1,'data'=>$coss,'msg'=>'收藏成功']);
		   }else{
                return json(['code'=>0,'msg'=>'收藏失败']);
		   }
		}
	}

	//用户收藏数量
	public function collnum(){
		if(request() -> isPost()){
			$uid = session('uids');
			if(!isset($uid)){
				return json(['code'=>0,'msg'=>'你还未登录,不能进行点击']);
			}
			$data['id'] = input('post.id');
            $num = db('common_block_shareurl')->where($data)->setInc('num', 1);
            if($num){
            	return json(['code'=>1,'msg'=>'点击次数添加成功']);
            }else{
            	return json(['code'=>0,'msg'=>'点击次数添加失败']);
            }
        }
	}

	//用户收藏网址信息
	public function urls(){
		if(request() -> isPost()){
			$data['id'] = input('post.id');
            $url = db('common_block_shareurl')->where($data)->find();
            if($url){
            	return json(['code'=>1,'data'=>$url,'msg'=>'存在']);
            }else{
            	return json(['code'=>0,'msg'=>'该网址不存在']);
            }
        }
	}

	//搜索信息
	public function searchs(){
		if(request() -> isPost()){
			$key = input('post.key');
			$list = db('common_block_shareurl')->field('id,title')->where('id|title','like','%'.$key.'%')->select();
			if($list){
				return json(['code'=>1,'data'=>$list,'msg'=>'存在信息']);
			}else{
				return json(['code'=>0,'msg'=>'数据为空']);
			}
		}
	}

	//编辑我的收藏标题
	public function editcoll(){
		if(request() ->isPost()){
			$data['uid'] = session('uids');
			$data['id'] = input('post.id');
			$titles = trim(input('post.titles'));
			$updates = db('common_collect')->where($data)->update(['titles'=>$titles]);
			if($updates){
				return json(['code'=>1,'titles'=>$titles,'msg'=>'编辑成功']);
			}else{
				return json(['code'=>0,'msg'=>'编辑失败']);
			}
		}
	}

	//删除我的收藏
	public function delcoll(){
		if(request() ->isPost()){
			$data['id'] = input('post.id');
			$del = db('common_collect')->where($data)->delete();
			if($del){
                return json(['code'=>1,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'删除失败']);
            }
		}
	}
    
}

